package com.hk.core.jdbc;

import com.hk.commons.query.AndOr;
import com.hk.commons.util.StringUtils;
import com.hk.core.jdbc.query.CompositeCondition;
import com.hk.core.jdbc.query.Condition;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.util.*;

/**
 * 更新参数
 *
 * @author kevin
 * @date 2018-09-19 14:01
 */
@Data
@Accessors(chain = true)
@NoArgsConstructor
public final class UpdateArguments {

    /**
     * 要更新的表名
     */
    private String from;

    /**
     * 要删除的表名
     */
    private Map<String, Object> sets = new HashMap<>();

    /**
     * 条件 where conditions
     */
    private CompositeCondition conditions = new CompositeCondition();

    public UpdateArguments addCondition(Condition condition) {
        conditions.addConditions(condition);
        return this;
    }

    public Map<String, Object> getSets() {
        return Collections.unmodifiableMap(sets);
    }

    public UpdateArguments addSets(Map<String, Object> sets) {
        if (Objects.nonNull(sets)) {
            this.sets.putAll(sets);
        }
        return this;
    }

    public UpdateArguments addSet(String key, Object value) {
        if (StringUtils.isNotEmpty(key)) {
            this.sets.put(key, value);
        }
        return this;
    }

    public UpdateArguments addConditions(Condition... conditions) {
        this.conditions.addConditions(conditions);
        return this;
    }

    public UpdateArguments addConditions(Collection<? extends Condition> conditions) {
        this.conditions.addConditions(conditions);
        return this;
    }

    public UpdateArguments removeCondition(Condition condition) {
        this.conditions.removeCondition(condition);
        return this;
    }

    public UpdateArguments clearConditions() {
        this.conditions.clearConditions();
        return this;
    }

    public UpdateArguments removeCondition(int index) {
        this.conditions.removeCondition(index);
        return this;
    }

    public UpdateArguments andOr(AndOr andOr) {
        this.conditions.setAndOr(andOr);
        return this;
    }
}

